<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>表单</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">

    <link rel="stylesheet" href="/Public/BeginnerAdmin/plugins/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="/Public/BeginnerAdmin/plugins/font-awesome/css/font-awesome.min.css">
</head>

<body>
<div style="margin: 15px;">
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>新增角色</legend>
    </fieldset>

    <form class="layui-form" action="/home/role/add_callback" method="POST" id="form1" enctype="multipart/form-data" >
        <div class="layui-form-item">
            <label class="layui-form-label">角色名称</label>
            <div class="layui-input-block">
                <input type="text" name="name" lay-verify="required" autocomplete="off" placeholder="请输入角色名称"
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <select name="status">
                    <option value="1">正常</option>
                    <option value="0">停用</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">选择父级</label>
            <div class="layui-input-block">
                <select name="pid">
                    <option value="0">顶级</option>
                    <volist name="data"  id="v"  key="key"  >
                        <option value="{$v.value}"><?php echo str_repeat('--',$v['level']);?> {$v.name}</option>
                    </volist>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">注释</label>
            <div class="layui-input-block">
                <input type="text" name="remark" lay-verify="required" autocomplete="off" placeholder="注释说明权限"
                       class="layui-input">
            </div>
        </div>

        <for start="0" end="$length" key="i">
            <if condition="$i eq  0">
                <div class="layui-form-item">
                    <label class="layui-form-label">节点</label>
                    <div class="layui-input-block">
                        <volist name="node_list"  id="v"  key="key" offset="6*$i"   length="6" >
                            <input type="checkbox" name="node_id[{$v.value}]" title="{$v.name}"><div class="layui-unselect layui-form-checkbox" lay-skin=""><span>{$v.name}{$v.id}</span><i class="layui-icon"></i></div>
                        </volist>
                    </div>
                </div>

            <else/>
                <div class="layui-form-item">
                    <label class="layui-form-label">&nbsp;</label>
                    <div class="layui-input-block">
                        <volist name="node_list"  id="v"  key="key" offset="6*$i"   length="6" >
                            <input type="checkbox" name="node_id[{$v.value}]" title="{$v.name}"><div class="layui-unselect layui-form-checkbox" lay-skin=""><span>{$v.name}</span><i class="layui-icon"></i></div>
                        </volist>
                    </div>
                </div>
            </if>

        </for>




        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="demo1" id="submit">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<script type="text/javascript" src="/Public/jquery/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="/Public/jquery/jquery.form.js"></script>
<script src="/Public/layui/dist/layui.all.js"></script>
<script>
    function ajaxCall()
    {
        window.parent.location.href=window.parent.location.href;
    }

</script>
<script>
    layui.use(['form', 'layedit','laydate','upload','jquery'], function () {
        var form = layui.form,
            layer = layui.layer,
            layedit = layui.layedit;
        var laydate = layui.laydate;
        var upload = layui.upload;
        var $ = layui.jquery;

        laydate.render({
            elem: '#date', //指定元素
            type: 'date'
        });

        //图片上传
        var uploadInst = upload.render({
            elem: '#test1' //绑定元素
            ,url: 'http://www.layui.test/home/article/uploadImage' //上传接口
            ,type: 'POST'
            ,done: function(res){
                //上传完毕回调
                $("img").attr({ src: res.data.src, alt: res.data.title });
                $('input[name=avatar]').val(res.data.src);
            }
            ,error: function(){
                //请求异常回调
            }
        });


        //监听提交 返回json数据
        form.on('submit(demo1)', function (data) {
//            console.log(JSON.stringify(data.field));
//            layer.alert(JSON.stringify(data.field), {
//                title: '最终的提交信息'
//            });
//            return false;
            //可选项
            var options = {
                beforeSubmit: showRequest,  //提交前的回调函数
                success: showResponse,      //提交后的回调函数
                error:showError,           //提交失败后回调函数
                timeout: 10000             //限制请求的时间，当请求大于10秒后，跳出请求
            }
            function showRequest(formData, jqForm, options){}
            function showResponse(responseText, statusText)
            {
                var error=responseText.error_code;
                var msg=responseText.msg;
                if(!msg){msg='网络异常,请稍候再试!';}
                if(error!='0'){layer.alert(msg, {icon: 2});return;}
                if(error == 0){
                    layer.msg(msg, {icon: 1},function(){
                        ajaxCall();
                    });
                    return;
                }
            }
            function showError(){
                var msg='网络异常,请稍候再试!';
                layer.alert(msg);
            }
            //ajax 提交
            $('#form1').ajaxSubmit(options);
            return false;
        });
    });
</script>

</body>

</html>